package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-12-10
 */
@Api(tags="讲师管理")
@RestController
@RequestMapping("/eduservice/eduteacher")
public class EduTeacherController {


    @Autowired
    private EduTeacherService eduTeacherService;

    //查询讲师表所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }


    //根据id删除讲师
    @CacheEvict(value = "'teacherandcourse'",allEntries = true)
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){

        return eduTeacherService.removeById(id)?R.ok():R.error();
    }


    /**
     * 分页查询讲师
     * @param current 当前页
     * @param limit   每页显示记录数
     * @return
     */
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable Long current,@PathVariable Long limit){
        Page<EduTeacher> teacherPage=new Page<>(current,limit);
        eduTeacherService.page(teacherPage);

        long total = teacherPage.getTotal();//总记录数
        List<EduTeacher> records = teacherPage.getRecords();//每页数据的list集合

        /*Map map = new HashMap();
        map.put("total",total);
        map.put("row",records);
        return R.ok().data(map);*/

        return R.ok().data("total",total).data("rows",records);
        //return R.ok().data("result",teacherPage);
    }

    /**
     * 条件查询带分页的方法
     * @param current 当前页
     * @param limit 每页显示记录数
     * @param teacherQuery 查询条件
     * @return
     */
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page=new Page<>(current,limit);

        /*String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        QueryWrapper<EduTeacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(name),"name",name);
        queryWrapper.eq(!StringUtils.isEmpty(level),"level",level);*/

        LambdaQueryWrapper<EduTeacher> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(teacherQuery.getName()!=null,EduTeacher::getName,teacherQuery.getName());
        lambdaQueryWrapper.eq(!StringUtils.isEmpty(teacherQuery.getLevel()),EduTeacher::getLevel,teacherQuery.getLevel());
        lambdaQueryWrapper.ge(!StringUtils.isEmpty(teacherQuery.getBegin()),EduTeacher::getGmtCreate,teacherQuery.getBegin());
        lambdaQueryWrapper.le(!StringUtils.isEmpty(teacherQuery.getEnd()),EduTeacher::getGmtModified,teacherQuery.getEnd());

        //排序
        lambdaQueryWrapper.orderByDesc(EduTeacher::getGmtCreate);
        eduTeacherService.page(page,lambdaQueryWrapper);

        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 添加讲师
     * @param eduTeacher
     * @return
     */
    @CacheEvict(value = "'teacherandcourse'",allEntries = true)
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        return save?R.ok():R.error();
    }

    /**
     * 修改讲师
     * @param eduTeacher
     * @return
     */
    @CacheEvict(value = "'teacherandcourse'",allEntries = true)
    @PutMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean update = eduTeacherService.updateById(eduTeacher);
        return update?R.ok():R.error();
    }

    /**
     * 根据id查讲师
     * @param id
     * @return
     */
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher",byId);
    }
}

